#ifndef QTAV_QML_AVPLAYER_H
#define QTAV_QML_AVPLAYER_H

#include <QmlAV/Export.h>
#include <QtCore/QObject>
#include <QmlAV/QQuickItemRenderer.h>

namespace QtAV {
class AVPlayer;
}
using namespace QtAV;
class QMLAV_EXPORT QmlAVPlayer : public QObject
{
    Q_OBJECT
    Q_PROPERTY(qreal volume READ volume WRITE setVolume NOTIFY volumeChanged)
    Q_PROPERTY(int duration READ duration NOTIFY durationChanged)
    Q_PROPERTY(int position READ position NOTIFY positionChanged)
    Q_PROPERTY(bool muted READ isMuted WRITE setMuted NOTIFY mutedChanged)
    Q_PROPERTY(PlaybackState playbackState READ playbackState NOTIFY playbackStateChanged)
    Q_PROPERTY(qreal speed READ speed WRITE setSpeed NOTIFY speedChanged)
    Q_PROPERTY(QUrl source READ source WRITE setSource NOTIFY sourceChanged)
    Q_ENUMS(PlaybackState)
public:
    enum PlaybackState {
        PlayingState,
        PausedState,
        StoppedState
    };

    explicit QmlAVPlayer(QObject *parent = 0);
    QUrl source() const;
    void setSource(const QUrl& url);
    QObject* videoOut();
    void setVideoOut(QObject* out);
    qreal volume() const;
    void setVolume(qreal volume);
    bool isMuted() const;
    void setMuted(bool m);
    int duration() const;
    int position() const;
    PlaybackState playbackState() const;
    void setPlaybackState(PlaybackState playbackState);
    qreal speed() const;
    void setSpeed(qreal s);
    Q_INVOKABLE void play(const QUrl& url);
    AVPlayer *player();

public Q_SLOTS:
    void play();
    void pause();
    void resume();
    void togglePause();
    void stop();
    void nextFrame();
    void seek(qreal position);
    void seekForward();
    void seekBackward();

Q_SIGNALS:
    void volumeChanged();
    void mutedChanged();
    void durationChanged();
    void positionChanged();
    void sourceChanged();
    void videoOutChanged();
    void playbackStateChanged();
    void speedChanged();
    void paused();
    void stopped();
    void playing();
private Q_SLOTS:
    void _q_started();
    void _q_stopped();
    void _q_paused(bool);

private:
    Q_DISABLE_COPY(QmlAVPlayer)
    PlaybackState mPlaybackState;
    QtAV::AVPlayer *mpPlayer;
    QUrl mSource;
};

#endif // QTAV_QML_AVPLAYER_H
